package com.tbit.uqbike.service.business.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.tbit.uqbike.object.pojo.RideCard;
import com.tbit.uqbike.service.baseAbstract.SynchronousService;
import com.tbit.uqbike.webmanager.dao.core.RideCardDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * 同步骑行卡配置
 * @author <a href="mailto:liu.haoxiang@tbit.com.cn">BUGXiang</a>
 * @version 1.0

 * @since 2025-2-21
 */
@Service("cyclingCardConfigService")
@Slf4j
public class CyclingCardConfigServiceImpl extends SynchronousService {

    @Resource
    private RideCardDao rideCardDao;

    @Override
    protected List<?> queryConfig(Integer accountId) {
        List<RideCard> rideCards = rideCardDao.selectByAccountId(accountId);
        if(CollectionUtil.isNotEmpty(rideCards)){
            return rideCards;
        }else{
            return Collections.emptyList();
        }
    }

    @Override
    protected void deleteConfig(Integer accountId) {
        List<RideCard> rideCards = rideCardDao.selectByAccountId(accountId);
        if(CollectionUtil.isNotEmpty(rideCards)){
            log.info("同步配置删除骑行卡配置:{}", JSON.toJSONString(rideCards));
            rideCardDao.UpdateByAccountId((accountId));
        }
    }

    @Override
    protected void insertConfig(List<?> objects, Integer accountId) {
        if (CollectionUtil.isEmpty(objects)){
            return;
        }
        List<RideCard> rideCards = (List<RideCard>) objects;
        rideCards.forEach(rideCard -> rideCard.setAccountId(accountId));
        rideCardDao.batchAdd(rideCards);
    }
}
